//
//  UserLicensePlateViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/26.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "UserLicensePlateViewController.h"
#import "RHTableView.h"
#import "LicensePlateTableViewCell.h"
#import "V4TestLicensePlateContentViewController.h"
#import "V4AddLicensePlateViewController.h"
#import "TopToggletabView.h"
#import "V4LicensePlateTableViewCell.h"


@interface UserLicensePlateViewController (){
    
    RHTableView *table_user;
    BOOL boolPopRefresh;
    NSString *strType;
}
@property(nonatomic,strong)TopToggletabView *topView;

@end

@implementation UserLicensePlateViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (boolPopRefresh) {
        boolPopRefresh=NO;
        [self loadTableData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    float fty=kTopHeight;

        strType=@"";
        [self rightButton:nil image:@"jia" sel:@selector(rightButtonClicked)];
        TopToggletabView *topView=[[TopToggletabView alloc] initWithFrame:CGRectMake(0, fty, kScreenWidth, 40)];
        [self.view addSubview:topView];
        [topView setTiltelArray:@[@{@"title":@"全部",@"id":@""},@{@"title":@"未审核",@"id":@"2"},@{@"title":@"已审核",@"id":@"1"},] toggleTab:^(NSDictionary *dicSelect) {
            strType=[dicSelect valueForJSONStrKey:@"id"];
            [self loadTableData];
        }];
    _topView=topView;
        fty+=40;
    
    table_user=[[RHTableView alloc]initWithFrame:CGRectMake(0, fty, kScreenWidth, H(self.view)-fty) style:UITableViewStyleGrouped];
    [table_user showRefresh:YES LoadMore:YES];
    table_user.delegate = self;
    table_user.dataSource = self;
    [table_user setBackgroundColor:rgbGray];
    table_user.sectionHeaderHeight = 0;
    table_user.sectionFooterHeight = 0;
    table_user.separatorInset=UIEdgeInsetsMake(0,0, 0, 0);
    [table_user setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:table_user];
    [table_user registerClass:[V4LicensePlateTableViewCell class] forCellReuseIdentifier:@"LicensePlateTableViewCell"];
    [table_user setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [self loadTableData];
    
}
//
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [table_user cancelDownload];
}

-(void)loadTableData{
//    krequestParam
//    [dictparam setValue:@"%@" forKey:@"page"];
//    [dictparam setValue:@"20" forKey:@"pagesize"];
//    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV3usermyproduct,dictparam.wgetParamStr];
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    if ([[Utility Share] userIsADD]) {
        [dictparam setValue:@"crm" forKey:@"type"];
        [dictparam setValue:strType forKey:@"isopen"];
    }else{
        [dictparam setValue:@"crm" forKey:@"type"];
        [dictparam setValue:strType forKey:@"isopen"];
    }
    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV4productslists,dictparam.wgetParamStr];
    [table_user refresh];
}
#pragma mark button
-(void)rightButtonClicked{
//     [self pushController:[V4AddLicensePlateViewController class] withInfo:nil withTitle:@"牌照上传" ];
      __weak __typeof(self) weakSelf = self;
    [self pushController:[V4AddLicensePlateViewController class] withInfo:nil withTitle:@"牌照上传" withOther:nil withAllBlock:^(id data, int status, NSString *msg) {
        if (status==200) {
            [weakSelf.topView clickatIndex:0];
        }
        
    }];
}
#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [table_user.dataArray  count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];
//    float fth=[self heightForLabel:kScreenWidth-50 font:fontTitle text:[dic valueForJSONStrKey:@"stitle"]];
//    fth = fth>20?40:20;
    return 15+12+15+12+10+12+10+10+10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.section];
    V4LicensePlateTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LicensePlateTableViewCell" forIndexPath:indexPath];
    cell.data = dic;
//    [cell setValueForDictionary:dic indexPath:indexPath type:[[Utility Share] userIsADD]?@"showSatus":@"user"];
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithDictionary:[table_user.dataArray objectAtIndex:indexPath.section]];
//    if ([[Utility Share] userIsADD]) {
//        krequestParam
//          __weak __typeof(self) weakSelf = self;
//        [dictparam setValue:[dic valueForJSONStrKey:@"id"] forKey:@"id"];
//        [NetEngine createPostAction:XKFV4productsdetails withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
//            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
//                [self pushController:[V4AddLicensePlateViewController class] withInfo:@"Edit" withTitle:@"编辑牌照" withOther:[resData objectForJSONKey:@"data"]  withAllBlock:^(id data, int status, NSString *msg) {
//                    if (status==200) {
//                        [weakSelf.topView clickatIndex:0];
//                    }
//                    
//                }];
//           
//            }else{
//                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
//            }
//        }];
//
//    }else{
//        [self pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic];
//    }
//    
    
    __weak __typeof(self) weakSelf = self;
     [self pushController:[V4TestLicensePlateContentViewController class] withInfo:@"Insurance" withTitle:@"牌照详情" withOther:dic  withAllBlock:^(id data, int status, NSString *msg) {
         if (status==200) {
             [weakSelf.topView clickatIndex:0];
         }
     }];
    
    if ([[dic valueForJSONStrKey:@"isread"] isEqualToString:@"0"]) {
        [dic setValue:@"1" forKey:@"isread"];
        [table_user.dataArray replaceObjectAtIndex:indexPath.section withObject:dic];
        [table_user reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

#pragma mark popRefresh
-(void)popRefreshData{
    boolPopRefresh=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
